package com.hmall.trade.listener;

import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.impl.OrderServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class PayStatusListener {

    private final OrderServiceImpl orderService;
    // 更新支付状态为成功
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("trade.pay.success.queue"),
            exchange = @Exchange("pay.direct"),
            key = "pay.success"
    ))
    public void markOrderPaySuccess(Long orderId){
        // 1. 获取订单支付状态
        Order order = orderService.getById(orderId);
        // 2. 判断订单支付状态是否为未支付
        if(order.getStatus() == null || order.getStatus() != 1){
            return;
        }
        // 3. 更新订单支付状态
        orderService.markOrderPaySuccess(orderId);
    }
}
